﻿using CoreShop.Model;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CoreShop.App
{
    public class VoiceHelp
    {
        private static SpeechSynthesizer synthesizer = new SpeechSynthesizer();

        private static AudioFileReader audioFileReader;

        private static WaveOutEvent audioWaveOut;

        public static void PlayVoice(string PayCodeName , decimal payAmt) 
        {
            if (AppConstVars.systemsetup.speechPayInfo == 0) return;

            string textToSpeak = "";
            if (!string.IsNullOrEmpty(PayCodeName))
            {
                textToSpeak = PayCodeName + "收款" + payAmt + "元";
            }
            else
            {
                textToSpeak = "总收款" + payAmt + "元";
            }
            Thread threadPrint = new Thread(PlaySpeechVoice);
            threadPrint.SetApartmentState(ApartmentState.STA);
            threadPrint.IsBackground = true;
            threadPrint.Start(textToSpeak);            
        }

        public static void PlayVoiceStr(string PayCodeName)
        {
            if (AppConstVars.systemsetup.speechPayInfo == 0) return;

            Thread threadPrint = new Thread(PlaySpeechVoice);
            threadPrint.SetApartmentState(ApartmentState.STA);
            threadPrint.IsBackground = true;
            threadPrint.Start(PayCodeName);           
        }

        /// <summary>
        /// Ai合成语音
        /// </summary>
        /// <param name="SeechValue"></param>
        public static void PlaySpeechVoice(object SeechValue)
        {
            try
            {
                if (synthesizer == null)
                {
                    // 创建SpeechSynthesizer对象
                    synthesizer = new SpeechSynthesizer();
                }
                if (audioFileReader != null)
                {
                    audioFileReader.Dispose();
                }
                if (!Directory.Exists($"{Application.StartupPath}\\voice")) 
                {
                    Directory.CreateDirectory($"{Application.StartupPath}\\voice");
                }
                string PlaySpeechValue = SeechValue != null ? SeechValue + "" : "";
                // 调整音量级别 
                // 设置音量级别，这里设置为最大值100
                synthesizer.Volume = 100;
                string OrgSelectname = synthesizer.Voice.Name;
                //string SelectVoiceName = SettingModelUtil.settingsModel.sOUND.VOICE;
                ////设置语音类型名称
                //if (!string.IsNullOrEmpty(SelectVoiceName) && OrgSelectname != SelectVoiceName)
                //{
                //    synthesizer.SelectVoice(SelectVoiceName);
                //}
                // 将文字转换为语音
                string FileName = $"{Application.StartupPath}\\voice\\SpeechWav.wav";
                // 设置生成的语音包文件名
                synthesizer.SetOutputToWaveFile(FileName);
                // 将文字转换为语音
                synthesizer.Speak(PlaySpeechValue);
                //清空占用
                synthesizer.SetOutputToNull();

                // 创建一个WaveOutEvent对象
                if (audioWaveOut == null)
                {
                    audioWaveOut = new WaveOutEvent();
                }

                //语音播放设备
                //string PlayVoiceVolumeValue = SettingModelUtil.settingsModel.sOUND.INPUTDEVICES;
                //if (!string.IsNullOrEmpty(PlayVoiceVolumeValue))
                //{
                //    // 设置 WaveOutEvent 实例的输出设备
                //    audioWaveOut.DeviceNumber = Convert.ToInt32(PlayVoiceVolumeValue);
                //}
                // 加载音频文件
                audioFileReader = new AudioFileReader(FileName);
                // 创建一个增益效果器对象
                var volumeProvider = new VolumeSampleProvider(audioFileReader.ToSampleProvider());
                // 将音频文件添加到WaveOutEvent对象中
                audioWaveOut.Init(volumeProvider);
                // 设置音量增益为2倍
                // 这里设置音量，接收的数值为单精度浮点数，经过测试，超过1倍以后，音量增加效果会递减
                volumeProvider.Volume = 100;// SettingModelUtil.settingsModel.sOUND.VOLUME <= 0 ? 10 : SettingModelUtil.settingsModel.sOUND.VOLUME;
                // 开始播放音频
                audioWaveOut.Play();
            }
            catch (Exception ex)
            {
                //Log.LogManage.Error("Ai语音合成播报出错:"  + ex);
            }
        }
    }
}
